package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.ActivityDto;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public PageResult<Activity> list(ActivityDto activityDto) {
        //创建PageHelper对象,设置起始页和每页记录数
        PageHelper.startPage(activityDto.getPage(), activityDto.getPageSize());

        //调用Mapper接口,执行分页查询
        List<Activity> activityList = activityMapper.selectList(activityDto);

        //封装PageResult对象并返回
        Page<Activity> p = (Page<Activity>) activityList;

        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void add(Activity activity) {
        //补全数据
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        //调用mapper接口
        activityMapper.insert(activity);
    }

    @Override
    public void deleteById(Integer id) {
        activityMapper.deleteById(id);
    }

    @Override
    public Activity getById(Integer id) {
        return activityMapper.selectById(id);
    }

    @Override
    public void update(Activity activity) {
        //补全更新时间
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> getByType(Integer type) {
        return activityMapper.selectByType(type);
    }
}
